package org.backend.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.backend.pojo.entity.Address;
import org.backend.pojo.entity.Transaction;
import org.backend.pojo.dto.TransactionCreateDTO;
import org.backend.pojo.vo.*;
import org.backend.service.AddressService;
import org.backend.service.TransactionService;
import org.backend.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.backend.utils.JwtUtil.getOpenId;
import static org.backend.utils.JwtUtil.setOpenId;

@RestController
@RequestMapping("/transaction")
@Slf4j
public class TransactionController {

    @Autowired
    private TransactionService transactionService;

    /**
     * 创建一个新订单
     *
     * @param transactionCreateDTO
     * @param request
     * @return
     */
    @PostMapping("/create")
    public ApiResponse<Integer> create(@RequestBody TransactionCreateDTO transactionCreateDTO, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        transactionCreateDTO = setOpenId(transactionCreateDTO, token);
        log.info(transactionCreateDTO.toString());
        return transactionService.create(transactionCreateDTO);
    }

    /**
     * 根据userId或者openid查询订单信息
     *
     * @param transaction
     * @param request
     * @return
     */
    @PostMapping("/getTransaction")
    public ApiResponse<List<TransactionWithProductsVO>> getTransaction(@RequestBody Transaction transaction, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        transaction = setOpenId(transaction, token);
        return transactionService.getTransaction(transaction);
    }

    /**
     * 根据userId或者openid查询对应的transactionId的订单详情
     * @param transaction
     * @param request
     * @return
     */
    @PostMapping("/getTransactionForOrder")
    public ApiResponse<TransactionWithProductsVO> getTransactionForOrder(@RequestBody Transaction transaction, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        transaction = setOpenId(transaction, token);
        return transactionService.getTransactionForOrder(transaction);
    }

    /**
     * 获取支付页面的详细交易信息
     * @param transaction
     * @param request
     * @return
     */
    @PostMapping("/getTransactionForPay")
    public ApiResponse<TransactionForPayVO> getTransactionForPay(@RequestBody Transaction transaction, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        transaction = setOpenId(transaction, token);
        return transactionService.getTransactionForPay(transaction);
    }

    /**
     * 根据交易Id删除交易记录（取消订单）
     * @param transaction
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public ApiResponse<String> delete(@RequestBody Transaction transaction, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        transaction = setOpenId(transaction, token);
        return  transactionService.delete(transaction);
    }

    /**
     * 支付订单
     * @param transaction
     * @param request
     * @return
     */
    @PutMapping("/payment")
    public ApiResponse<String> payment(@RequestBody Transaction transaction, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        transaction = setOpenId(transaction, token);
        return transactionService.payment(transaction);
    }

    @PostMapping("/chart")
    public ApiResponse<List<MonthStatisticsVO>> chart(@RequestParam int year, @RequestParam int month, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String openid = getOpenId(token);
        return ApiResponse.success(transactionService.chart(openid, year, month));
    }

    @PostMapping("/categoryChart")
    public ApiResponse<List<CategoryStatisticsVO>> categoryChart(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String openid = getOpenId(token);
        return ApiResponse.success(transactionService.categoryChart(openid));
    }
}
